package com.example.springboard.controller;

import com.example.springboard.service.MyUserService;
import com.example.springboard.service.RedisService;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@RestController
@RequestMapping("/springboard")
public class TestController {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    RedisService redisService;


//    @GetMapping(value = "/test",produces = "application/json;charset=UTF-8")
    @GetMapping(value = "/test")
    @ResponseStatus(HttpStatus.OK)
    public String test() {
        enum Colors {
            RED, GREEN, BLUE, YELLOW, ORANGE;
        }
        logger.debug(Arrays.toString(Colors.values()));
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
        Map<String, Integer> ages = Map.of("Alice", 25, "Bob", 30, "Charlie", 35);
        Set<String> cities = new HashSet<>();
        cities.add("New York");
        cities.add("Los Angeles");
        cities.add("Chicago");
        System.out.println(cities);
        StringBuilder result = new StringBuilder();
        // 使用 Lambda 表达式遍历列表
        names.forEach(name -> result.append("英文名: ").append(name).append("\n"));
        names.forEach(System.out::println);
//        System.out.println(result);
        return "ok";
    }

    @Getter
    @Setter
    class Person {
        private String name;
        private int age;
        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }
    }

    @PostMapping("/redis/set")
    public String set(@RequestParam("key") String key, @RequestParam("value") String value) {
        redisService.set(key, value);
        return Objects.requireNonNullElse(value, "null");
    }

    @GetMapping("/redis/get")
    public String get(@RequestParam("key") String key) {
        String value = redisService.get(key);
        return Objects.requireNonNullElse(value, "null");
    }

    @PostMapping("/redis/hset")
    public String hset(@RequestParam("key") String key, @RequestParam("field") String field,@RequestParam("value") String value) {
        redisService.hset(key, field, value);
        return Objects.requireNonNullElse(value, "null");
    }

    @GetMapping("/redis/hget")
    public String hget(@RequestParam("key") String key,@RequestParam("field") String field) {
        String value = redisService.hget(key,field);
        return Objects.requireNonNullElse(value, "null");
    }
}
